<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-19
 * Time: 下午2:37
 * 微信用户授权处理--获取微信用户相关信息
 */
namespace Home\Controller;
use Think\Controller;

class OauthController extends Controller{
    public function getcode(){
        $uri    =   $_SERVER['REQUEST_URI'];
        //获取code   REQUEST_URIl：/Home/Oauth/getcode?code=041IA6Kb0s0HOv1B3vLb0CvTJb0IA6KW&state=0
        $start  =   strpos($uri, "=");
        $end    =   strpos($uri, "&");
        $len    =   $end-$start-1;
        $code   =   substr($uri,$start+1, $len);
        //获取 state
        $start  =   strrpos($uri, "=");
        $state  =   substr($uri, $start+1);

        $refurl =   U('Home/Index/index');  //操作成功之后 跳转到 贴吧首页
        $wxaccount = M('wxaccount')->where(array('wx_id'=>1))->find();

        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$wxaccount['wx_appid']}&secret={$wxaccount['wx_appsecret']}&code={$code}&grant_type=authorization_code";
        $jsoninfo = $this->getdata($url);

        $user = array();
        $user['openid'] = $jsoninfo['openid'];

        $userinfo = M('users')->where($user)->find();
        if(!empty($userinfo)){
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$jsoninfo['access_token']}&openid={$userinfo['openid']}";
            $memberinfo = $this->getdata($url);

            $update = array();
            $update['headimgurl']   = $memberinfo['headimgurl'];
            $update['nickname']     = $memberinfo['nickname'];
            $update['sex']          = $memberinfo['sex'];

            // $data = $this->getUserInfo($user['openid']);

            M('users')->where(array('user_id'=>$userinfo['user_id']))->save($update);
            cookie('user_id',$userinfo['user_id'], time()+3600*24*30);
            header("Location:{$refurl}");
            exit;
        }else{
            $user['access_token']   = $jsoninfo['access_token'];
            $user['add_time']       = time();
            $res = M('users')->add($user);

            if($res){
                $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$user['access_token']}&openid={$user['openid']}";
                $memberinfo = $this->getdata($url);

                $update = array();
                $update['headimgurl']   = $memberinfo['headimgurl'];
                $update['nickname']     = $memberinfo['nickname'];
                $update['sex']          = $memberinfo['sex'];

                // $data = $this->getUserInfo($user['openid']);
                // $update = array_merge($update,$data);

                M('users')->where(array('user_id'=>$res))->save($update);
                //$_SESSION['user_id'] = $userinfo['user_id'];
                cookie('user_id',$userinfo['user_id'], time()+3600*24*30);

                header("Location:{$refurl}");
                exit;
            }else{
                exit;
            }
        }
    }

    /**
     * 通过curl发送请求
     * @param $url
     * @return mixed
     */
    public function getdata($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $jsoninfo = json_decode($output, true);

        return $jsoninfo;
    }
} 